package com.jsoft.service;

import com.jsoft.dto.MenuDto;
import com.jsoft.dto.MenuGroupDto;
import com.jsoft.manager.MenuManager;
import com.jsoft.manager.SectorManager;
import com.jsoft.model.MenuDao;
import com.jsoft.model.MenuGroupDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Date: 28.09.13
 * Time: 10:47
 */

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuManager menuManager;
    @Autowired
    private SectorManager sectorManager;

    @Override
    @Transactional(readOnly = true)
    public List<MenuDto> getList() {
        List<MenuDao> menuDaoList = menuManager.getAllList();
        List<MenuDto> menuDtoList = new ArrayList<MenuDto>();
        for (MenuDao dao : menuDaoList) {
            menuDtoList.add(dao.wrapMenuDto());
        }
        return menuDtoList;
    }

    @Override
    @Transactional(readOnly = true)
    public MenuDto getMenuBySectorId(Integer sectorId) {
        MenuDao menu = sectorManager.getById(sectorId).getMenu();
        MenuDto menuDto = menu.wrapMenuDto();
        List<MenuGroupDao> menuGroupDaoList = menu.getMenuGroupList();
        List<MenuGroupDto> menuGroupDtoList = new ArrayList<MenuGroupDto>();
        for (MenuGroupDao groupDao : menuGroupDaoList) {
            menuGroupDtoList.add(groupDao.wrapMenuGroupDto());
        }
        menuDto.setMenuGroupList(menuGroupDtoList);
        return menuDto;
    }
}
